package com.studyyoun.demoandroidapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.StandardMessageCodec;


/**
 * Android  Flutter 消息通信
 */

public class FlutterMessageActivity extends AppCompatActivity implements View.OnClickListener {
	
	
	private BasicMessageChannel<Object> mObjectBasicMessageChannel;
	private TextView mOpenFlutterTextView3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_flutter);
		
		//点击加载 Flutter Widget
		TextView openFlutterTextView1 = findViewById(R.id.bt_open_1);
		//向 Flutter项目中发送消息
		TextView openFlutterTextView2 = findViewById(R.id.bt_open_2);
		//接收 Flutter 中的消息
		mOpenFlutterTextView3 = findViewById(R.id.bt_open_3);
		
		openFlutterTextView1.setOnClickListener(this);
		openFlutterTextView2.setOnClickListener(this);
		
		
		//创建引擎
		FlutterEngine lFlutterEngine = new FlutterEngine(this);
		//设置一些参数
		lFlutterEngine.getNavigationChannel().setInitialRoute("/test");
		//设置消息通道
		setMessageChannel(lFlutterEngine);
		//设置缓存
		lFlutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
		FlutterEngineCache.getInstance().put("test_engine", lFlutterEngine);
		
	}
	
	private void setMessageChannel(FlutterEngine flutterEngine) {
		//信息编码
		mObjectBasicMessageChannel = new BasicMessageChannel<Object>(
				flutterEngine.getDartExecutor().getBinaryMessenger(), //Flutter 引擎
				"101",  //通道标识
				StandardMessageCodec.INSTANCE);
		//设置监听
		mObjectBasicMessageChannel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() {
			@Override
			public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply<Object> reply) {
				Log.d("message","接收到Flutter中的消息"+message.toString());
				mOpenFlutterTextView3.setText(message.toString());
				
				//发送一个回执 只能使用一次
				reply.reply("收到消息了...");
			}
		});
		
	}
	
	
	private void openFunction3() {
		FlutterFragment lFlutterFragment = FlutterFragment
				.withCachedEngine("test_engine")
				.build();
		
		getSupportFragmentManager()
				.beginTransaction()
				.add(R.id.fl_open_flutter, lFlutterFragment)
				.commit();
	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.bt_open_1:
				openFunction3();
				break;
			case R.id.bt_open_2:
				//向Flutter 发送消息
				mObjectBasicMessageChannel.send("asd304", new BasicMessageChannel.Reply<Object>() {
					@Override
					public void reply(@Nullable Object reply) {
						String result ="";
						if(reply!=null){
							result = reply.toString();
						}
						Log.d("message","Android - 接收到回执"+result);
					}
				});
				break;
		}
		
	}
}